<template>
  <div class="login">
    <div class="headerLogin">
        <div class="logo">
           <!-- <img src="@/assets/images/sale/img/logoSale.png" alt=""> -->
           <!-- <svg-icon icon-class="logoSass" class-name="logoSale-icon" /> -->
        </div>
        <!-- <div class="line"> -->
           <!-- <svg-icon icon-class="lineWhite" class-name="lineWhite-icon" /> -->
        <!-- </div> -->
        <div class="titlelogo">
            <!-- <img src="@/assets/images/sale/img/titleSale.png" alt=""> -->
            <!-- <svg-icon icon-class="titleSass" class-name="titleSale-icon" /> -->
        </div>
    </div>
    <el-form ref="loginForm" :model="loginForm" :rules="loginRules" class="login-form">
      <h3 class="title">账号登录</h3>
<!--      <el-form-item prop="tenantId">-->
<!--        <el-input v-model="loginForm.tenantId" type="text" auto-complete="off" placeholder="请输入企业账号">-->
<!--          &lt;!&ndash; <svg-icon slot="prefix" icon-class="enterprise" class="el-input__icon input-icon" /> &ndash;&gt;-->
<!--          <span slot="prefix">企业账号</span>-->
<!--        </el-input>-->
<!--      </el-form-item>-->
      <el-form-item prop="username">
        <el-input
          v-model="loginForm.username"
          type="text"
          auto-complete="off"
          placeholder="请输入账号"
        >
          <span slot="prefix">账号</span>
          <!-- <svg-icon slot="prefix" icon-class="user" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="password">
        <el-input
          v-model="loginForm.password"
          type="password"
          auto-complete="off"
          placeholder="请输入密码"
          @keyup.enter.native="handleLogin"
        >
          <span slot="prefix">密码</span>
          <!-- <svg-icon slot="prefix" icon-class="password" class="el-input__icon input-icon" /> -->
        </el-input>
      </el-form-item>
      <el-form-item prop="code" v-if="captchaEnabled">
        <el-input
          class="code"
          v-model="loginForm.code"
          auto-complete="off"
          placeholder="请输入验证码"
          @keyup.enter.native="handleLogin"
        >
          <!-- <svg-icon slot="prefix" icon-class="validCode" class="el-input__icon input-icon" /> -->
          <img :src="codeUrl" @click="getCode" class="login-code-img" slot="suffix"/>
        </el-input>
        <!-- <div class="login-code">
          <img :src="codeUrl" @click="getCode" class="login-code-img"/>
        </div> -->
      </el-form-item>
      <el-checkbox v-model="loginForm.rememberMe" style="margin:0px 0px 25px 0px;">记住密码</el-checkbox>
      <el-form-item style="width:100%;">
        <el-button
          :loading="loading"
          size="medium"
          type="primary"
          style="width:100%;height:54px;font-size:18px;"
          @click.native.prevent="handleLogin"
        >
          <span v-if="!loading">登 录</span>
          <span v-else>登 录 中...</span>
        </el-button>
<!--        <div style="float: right;" v-if="register">-->
<!--          <router-link class="link-type" :to="'/register'">立即注册</router-link>-->
<!--        </div>-->
      </el-form-item>
    </el-form>
    <!--  底部  -->
    <!-- <div class="el-login-footer">
      <span>北京华信瑞德信息技术有限公司技术支持</span>
    </div> -->
  </div>
</template>

<script>
import {getCodeImg} from "@/api/login";
import Cookies from "js-cookie";
import {decrypt, encrypt} from "@/utils/jsencrypt";
import {regCLNS} from "@/utils/validate";

export default {
  name: "Login",
  data() {
    return {
      codeUrl: "",
      loginForm: {
        tenantId: "",
        username: "",
        password: "",
        rememberMe: false,
        code: "",
        uuid: ""
      },
      loginRules: {
        tenantId: [
          { required: true, trigger: "blur", message: "请输入您的企业账号" }
        ],
        username: [
          { required: true, trigger: "blur", message: "请输入您的账号" }
        ],
        password: [
          { required: true, trigger: "blur", message: "请输入您的密码" }
        ],
        code: [{ required: true, trigger: "change", message: "请输入验证码" }]
      },
      loading: false,
      // 验证码开关
      captchaEnabled: true,
      // 注册开关
      register: false,
      redirect: undefined
    };
  },
  watch: {
    $route: {
      handler: function(route) {
        this.redirect = route.query && route.query.redirect;
      },
      immediate: true
    }
  },
  created() {
    this.getCode();
    this.getCookie();
  },
  methods: {
    getCode() {
      getCodeImg().then(res => {
        this.captchaEnabled = res.data.captchaEnabled === undefined ? true : res.data.captchaEnabled;
        if (this.captchaEnabled) {
          this.codeUrl = "data:image/gif;base64," + res.data.img;
          this.loginForm.uuid = res.data.uuid;
        }
      });
    },
    getCookie() {
      const tenantId = Cookies.get('tenantId')
      const username = Cookies.get("username");
      const password = Cookies.get("password");
      const rememberMe = Cookies.get('rememberMe')
      this.loginForm = {
        tenantId: tenantId === undefined ? this.loginForm.tenantId : tenantId,
        username: username === undefined ? this.loginForm.username : username,
        password: password === undefined ? this.loginForm.password : decrypt(password),
        rememberMe: rememberMe === undefined ? false : Boolean(rememberMe)
      };
    },
    handleLogin() {
      this.$refs.loginForm.validate(valid => {
        if (valid) {
          this.loading = true;
          if (this.loginForm.rememberMe) {
            Cookies.set("tenantId", this.loginForm.tenantId, { expires: 30 });
            Cookies.set("username", this.loginForm.username, { expires: 30 });
            Cookies.set("password", encrypt(this.loginForm.password), { expires: 30 });
            Cookies.set('rememberMe', this.loginForm.rememberMe, { expires: 30 });
          } else {
            Cookies.remove("tenantId");
            Cookies.remove("username");
            Cookies.remove("password");
            Cookies.remove('rememberMe');
          }
          this.$store.dispatch("Login", this.loginForm).then(() => {
            // 登录接口调用后提示弹框
            // 如果密码强度符合直接执行跳转
            if (regCLNS(this.loginForm.password)) {
              // 注意：要和原页面跳转方法一致
              this.$router.push({ path: this.redirect || "/" })
            } else {
              this.$confirm("登录密码太简单了，请修改！", "提示", {
                confirmButtonText: "马上修改",
                cancelButtonText: "继续使用",
                closeOnClickModal: false,
                showClose: false,
                type: "warning"
              }).then(() => {
                this.$router.push({
                  path: "/user/profile", // 修改密码页面地址
                  query: {
                    actTab: "resetPwd"
                  }
                })
              }).catch(() => {
                // 注意：要和原页面跳转方法一致
                this.$router.push({path: this.redirect || "/"}).catch(() => {});
              });
            }
          }).catch(() => {
            if (this.captchaEnabled) {
              this.getCode();
            }
          }).finally(() => {
            this.loading = false;
          });
        }
      });
    }
  }
};
</script>

<style rel="stylesheet/scss" lang="scss">
.login {
  box-sizing: border-box;
  display: flex;
  justify-content: flex-end;
  align-items: center;
  height: 100%;
  background-image: url("../assets/images/sassBg.png");
  background-size: 100% 100%;
  padding-right:11%;
  position: relative;
  .headerLogin{
    width: 468px;
    height:70px ;
    box-sizing: border-box;
    display: flex;
    align-items: center;
    position: absolute;
    top:10vh;
    left: 9%;
    z-index: 22;
    .logo{
      box-sizing: border-box;
      img{
        width: 222px;
        height: 70px;
        vertical-align: middle;
      }
      .logoSale-icon{
        width: 222px;
        height: 70px;
        color: #fff;
      }
    }
    .line{
      box-sizing: border-box;
      margin: 0 20px;
      margin-top: 3px;
      width: 4px;
      height: 46px;
      background: #fff;
      .lineWhite-icon{
        width: 4px;
        height: 72px;
        color: #fff;
      }

    }
    .titlelogo{
      box-sizing: border-box;
      width: 222px;
      height: 70px;
      display: flex;
      align-items: center;
      padding-bottom: 5px;
      img{
        width: 201px;
        height: 47px;
        vertical-align: middle;
      }
      .titleSale-icon{
        width: 201px;
        height: 47px;
        color: #fff;
      }

    }
  }
}
.title {
  margin: 0px auto 30px auto;
  text-align: left;
  color: #333333;
  font-size: 18px;
}

.login-form {
  border-radius: 6px;
  background: #ffffff;
  width: 468px;
  padding: 52px 49px 22px 49px;
  .el-form-item{
    margin-bottom: 20px;
  }
  .el-input {
    height: 54px;
    input {
      height: 54px;
    }
    input::placeholder{
      color:#999999 ;
    }
    &::placeholder{
      color:#999999 ;
    }
  }
  .input-icon {
    height: 39px;
    width: 14px;
    margin-left: 2px;
  }
  .el-input__prefix{
    margin-left: 20px;
    color: #333333;
    top:9px;
  }
  .el-input__inner{
    padding-left: 110px !important;
    border: 1px solid #D5D3D3;
  }
  .code{
    .el-input__inner{
      padding-left: 23px !important;
    }
    .el-input__suffix{
      top:6px !important;
    }
  }
}
.login-tip {
  font-size: 13px;
  text-align: center;
  color: #bfbfbf;
}
.login-code {
  width: 33%;
  height: 38px;
  float: right;
  img {
    cursor: pointer;
    vertical-align: middle;
  }
}
.el-login-footer {
  height: 40px;
  line-height: 40px;
  position: fixed;
  bottom: 0;
  width: 100%;
  text-align: center;
  color: #fff;
  font-family: Arial;
  font-size: 12px;
  letter-spacing: 1px;
}
.login-code-img {
  height: 42px;
  border-radius: 2px;
}

</style>
